home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 34.zip / BS1 part 34 / FredFish PD 307.adf / FileIO / main.c < prev    next >
C/C++ Source or Header  |  1990-01-13  |  9KB  |  323 lines

  1. #include <functions.h>
  2. #include <exec/types.h>
  3. #include <exec/io.h>
  4. #include <stdio.h>
  5. #include <exec/memory.h>
  6. #include <intuition/intuition.h>
  7. #include <intuition/intuitionbase.h>
  8. #include <graphics/gfxbase.h>
  9. #include "FileIO.h"
  10.  
  11. /* extern long GetMsg(), OpenWindow(), IoErr(), OpenLibrary(); */
  12.  
  13. /* === System Global Variables ========================================== */
  14. struct IntuitionBase *IntuitionBase = 0L;
  15. struct GfxBase       *GfxBase  = 0L;
  16. struct DosLibrary    *DosBase  = 0L;
  17. struct RequesterBase *RequesterBase = 0L;
  18. struct FileIO        *myFileIO  = 0L;
  19. struct FileIO        *myFileIO2 = 0L;
  20. struct Window        *myWindow  = 0L;
  21. struct Screen        *myScreen  = 0L;
  22. ULONG  argcount;  /* Saves argc from main(). argcount==0, then run from WB. */
  23.  
  24. struct TextAttr  topaz80_font;
  25.  
  26. BOOL TestFileIO();
  27.  
  28. struct NewScreen NewFileIOScreen =
  29.    {
  30.    0, 0,           /* LeftEdge, TopEdge */
  31.    640, 400,       /* Width, Height */
  32.    2,              /* Depth */
  33.    0, 1,           /* Detail/BlockPens */
  34.    HIRES | LACE,   /* ViewPort Modes (must set/clear HIRES as needed) */
  35.    CUSTOMSCREEN,
  36.    &topaz80_font,  /* Font */
  37.    (UBYTE *)"Example FileIO Program's Screen",
  38.    0L,             /* Gadgets */
  39.    0L,             /* CustomBitMap */
  40.    };
  41.  
  42. struct NewWindow NewFileIOWindow =
  43.    {
  44.    168, 30,            /* LeftEdge, TopEdge */
  45.    303, 145,          /* Width, Height */
  46.    -1, -1,            /* Detail/BlockPens */
  47.    MENUPICK | MOUSEBUTTONS | CLOSEWINDOW | RAWKEY | DISKINSERTED,
  48.                       /* IDCMP Flags */
  49.    WINDOWDRAG | WINDOWDEPTH | SIZEBRIGHT | SMART_REFRESH |
  50.    WINDOWCLOSE | ACTIVATE | NOCAREREFRESH,
  51.                       /* Window Specification Flags */
  52.    0L,                /* FirstGadget */
  53.    0L,                /* Checkmark */
  54.    (UBYTE *)"FileIO Requester Window",  /* WindowTitle */
  55.    0L,                /* Screen */
  56.    0L,                /* SuperBitMap */
  57.    303, 145,          /* MinWidth, MinHeight */
  58.    600, 200,          /* MaxWidth, MaxHeight */
  59.    WBENCHSCREEN,
  60.    };
  61.  
  62.  
  63. struct IntuiText itxt[] = { 
  64.   {0,1,JAM1,19,1,NULL,(UBYTE *)"Multiple"},
  65.   {0,1,JAM1,19,1,NULL,(UBYTE *)"Handler"}, };
  66.  
  67. struct MenuItem mi[] = {
  68.   {&mi[1],0, 0,91,10,CHECKIT | MENUTOGGLE | ITEMTEXT | ITEMENABLED | HIGHCOMP,\
  69.    NULL,(APTR)&itxt[0],NULL,NULL},
  70.  
  71.   {NULL,0, 10,91,10,CHECKIT | MENUTOGGLE | ITEMTEXT | ITEMENABLED | HIGHCOMP,\
  72.    NULL,(APTR)&itxt[1],NULL,NULL},};
  73.  
  74. struct Menu myMenu = {NULL,0,0,72,0,MENUENABLED,  "Project",&mi[0]};
  75.  
  76.  
  77. BOOL myFunc();
  78.  
  79.  
  80. struct HandlerBlock hblock = {
  81.    /* APTR  StartUpCode; */       (APTR)myFunc,
  82.    /* APTR  DiskInsertedCode; */  0, 
  83.    /* APTR  GadgetCode; */        0, 
  84.    /* APTR  KeyCode; */           0,
  85.    /* APTR  MouseMoveCode; */     0 };
  86.  
  87.  
  88.  
  89. /************************ MAIN ROUTINE *****************************/
  90.  
  91. VOID main(argc, argv)
  92. LONG argc;
  93. char **argv;
  94. {
  95.    LONG   class;
  96.     USHORT code;
  97.    struct IntuiMessage *message;
  98.    BOOL   end;
  99.  
  100.    argcount = argc;
  101.  
  102.    if (!(IntuitionBase = (struct IntuitionBase *)
  103.       OpenLibrary("intuition.library", 0L)))
  104.       exit_program("FileIO Demo: No intuition library. \n", 1L);
  105.  
  106.    if (!(GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 0L)))
  107.       exit_program("FileIO Demo: No graphics library. \n", 2L);
  108.  
  109.    /* NOW OPEN THE REQUESTER LIBRARY */
  110.  
  111.    if (!(RequesterBase = (struct RequesterBase *)OpenLibrary("requester.library", 1L)))
  112.       exit_program("FileIO Demo: No requester.library. \n", 4L);
  113.  
  114.    if (argv)
  115.       {
  116.       /* OK, we started from CLI */
  117.       if (argc > 1)
  118.          {
  119.          if (myScreen = OpenScreen(&NewFileIOScreen))
  120.             {
  121.             NewFileIOWindow.Screen = myScreen;
  122.             NewFileIOWindow.Type = CUSTOMSCREEN;
  123.             }
  124.          }
  125.       }
  126.  
  127.    if (!(myWindow = (struct Window *) OpenWindow( &NewFileIOWindow ) ))
  128.       exit_program("FileIO Demo: Null Window.\n", 5L);
  129.  
  130.  
  131.     SetMenuStrip( myWindow, &myMenu );
  132.  
  133.  /* GET 2 FileIO STRUCTURES */
  134.  
  135.    if (!(myFileIO = GetFileIO() ))
  136.       exit_program("FileIO Demo: No FileIO 1.\n", 6L);
  137.  
  138.    if (!(myFileIO2 = GetFileIO() ))
  139.       exit_program("FileIO Demo: No FileIO 2.\n", 7L);
  140.  
  141.   /* Set up the XY co-ordinates where the requester should open */
  142.  
  143.   myFileIO->X = 6;
  144.   myFileIO->Y = 11;
  145.  
  146.   myFileIO2->X = 6;
  147.   myFileIO2->Y = 11;
  148.  
  149.  /* Set default colors and DrawMode */
  150.   myFileIO->DrawMode = JAM2;
  151.   myFileIO->PenA = 1;
  152.   myFileIO->PenB = 0;
  153.   myFileIO2->DrawMode = JAM2;
  154.   myFileIO2->PenA = 1;
  155.   myFileIO2->PenB = 0;
  156.  
  157.           /* pretty easy to set up, eh?  */
  158.  
  159.    end = FALSE;
  160.  
  161.    SetFlag(myFileIO2->Flags, WBENCH_MATCH | MATCH_OBJECTTYPE);
  162.    myFileIO2->MatchType = WBTOOL;
  163.    SetFlag(myFileIO2->Flags, USE_DEVICE_NAMES);
  164.  
  165.    myFileIO->Custom = &hblock;
  166.     SetFileIOHandlers( &hblock );
  167.  
  168.    while (end == FALSE)
  169.     {
  170.       WaitPort(myWindow->UserPort);
  171.  
  172.       while (message = ( struct IntuiMessage *)GetMsg(myWindow->UserPort))
  173.        {
  174.            code = message->Code;
  175.          class = message->Class;
  176.          ReplyMsg(message);
  177.  
  178.          switch (class)
  179.           {
  180.             case CLOSEWINDOW:
  181.                end = TRUE;
  182.                break;
  183.  
  184.             case DISKINSERTED:
  185.                /* You should clear the NO_CARE_REDRAW flag whenever you
  186.                 * detect that a new disk was inserted (if using this feature).
  187.                   We aren't using it, so comment it out.
  188.                ClearFlag(myFileIO->Flags, NO_CARE_REDRAW);
  189.                ClearFlag(myFileIO2->Flags, NO_CARE_REDRAW);
  190.                 */
  191.                break;
  192.  
  193.                 case MENUPICK:
  194.                switch( ITEMNUM( code ) )
  195.                     {
  196.                         case 0: 
  197.                             ToggleFlag( myFileIO->Flags, MULTIPLE_FILES );
  198.                        if (TestFileIO(myFileIO, myWindow))
  199.                        {
  200.                           /*
  201.                           ClearFlag(myFileIO->Flags, NO_CARE_REDRAW);
  202.                           ClearFlag(myFileIO2->Flags, NO_CARE_REDRAW);
  203.                               */
  204.                        }
  205.                             break;
  206.  
  207.                         case 1: 
  208.                             ToggleFlag( myFileIO->Flags, CUSTOM_HANDLERS );
  209.                        if (TestFileIO(myFileIO, myWindow))
  210.                        {
  211.                           /*
  212.                           ClearFlag(myFileIO->Flags, NO_CARE_REDRAW);
  213.                           ClearFlag(myFileIO2->Flags, NO_CARE_REDRAW);
  214.                               */
  215.                        }
  216.                             break;
  217.                     }    
  218.                     break;
  219.  
  220.             case MOUSEBUTTONS:
  221.                if (TestFileIO(myFileIO, myWindow))
  222.                {
  223.                   /*
  224.                   ClearFlag(myFileIO->Flags, NO_CARE_REDRAW);
  225.                   ClearFlag(myFileIO2->Flags, NO_CARE_REDRAW);
  226.                   */
  227.                }
  228.                break;
  229.  
  230.             case RAWKEY:
  231.                if (TestFileIO(myFileIO2, myWindow))
  232.                {
  233.                   /*
  234.                   ClearFlag(myFileIO->Flags, NO_CARE_REDRAW);
  235.                   ClearFlag(myFileIO2->Flags, NO_CARE_REDRAW);
  236.                   */
  237.                }
  238.                break;
  239.  
  240.             default:
  241.                break;
  242.           }
  243.        }
  244.     }
  245.   exit_program( 0L, 0L);
  246. }
  247.  
  248.  
  249.  
  250. exit_program( error_words, error_code )      /* All exits through here. */
  251. char  error_words;
  252. ULONG error_code;
  253. {
  254.    if( argcount && error_words ) puts( error_words );
  255.    if (myFileIO)  ReleaseFileIO(myFileIO);
  256.    if (myFileIO2) ReleaseFileIO(myFileIO2);
  257.  
  258.    if (myWindow) CloseWindow(myWindow);
  259.    if (myScreen) CloseScreen(myScreen);
  260.  
  261.    if (IntuitionBase) CloseLibrary(IntuitionBase);
  262.    if (GfxBase)       CloseLibrary(GfxBase);
  263.    if (DosBase)       CloseLibrary(DosBase);
  264.    if (RequesterBase) CloseLibrary(RequesterBase);
  265.    exit( error_code );
  266. }
  267.  
  268.  
  269.  
  270. BOOL TestFileIO(fileio, wind)
  271. struct FileIO *fileio;
  272. struct Window *wind;
  273. {
  274.  UBYTE *address;
  275.  struct FileEntry *fentry1 = 0, *fentry2 = 0;
  276.  
  277. /* This guy calls DoFileIO(), displays the file name selected by the
  278.  * user, and returns TRUE if the user swapped disks during DoFileIO()
  279.  * (else returns FALSE) if an error, CANCEL, or no disk swap.
  280.  */
  281.    UBYTE buffer[80];
  282.  
  283.    fileio->Buffer = buffer;
  284.  
  285.    address = DoFileIO(fileio, wind);
  286.  
  287.    if( address == buffer )
  288.    {
  289.       /* If user was positive and no error, display the name */
  290.       AutoMessage( buffer, wind);
  291.         if( fileio->Flags & MULTIPLE_FILES )
  292.         {
  293.          while( fentry2 = RetrieveEntry( &fentry1, fileio ) )
  294.             {
  295.                 AutoMessage( fentry2->filePart->EntryString, wind );
  296.             }
  297.         }
  298.    }
  299.  
  300.    if (!address)    AutoMessage("Error in operation", wind);
  301.    if (address==-1) AutoMessage("Cancelled operation", wind);
  302.  
  303.    if (FlagIsSet(fileio->Flags, DISK_HAS_CHANGED))
  304.       {
  305.       ClearFlag(fileio->Flags, DISK_HAS_CHANGED);
  306.       return(TRUE);
  307.       }
  308.    else
  309.      return(FALSE);
  310. }
  311.  
  312.  
  313. BOOL myFunc( extra,seconds,micros,mouseX,mouseY,fileio,xwind,iaddress )
  314. ULONG extra;
  315. APTR iaddress;
  316. struct Window *xwind;
  317. struct FileIO *fileio;
  318. SHORT mouseY, mouseX;
  319. ULONG micros,seconds;
  320. {
  321.      return(AutoPrompt3( "This is a custom StartHandler",0,0, xwind ));
  322. }
  323.